package application.gui.main.startWindow;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import application.LoginAndAdministrationModule.Registration;

public class RegisterPanel extends JPanel{

	public RegisterPanel(Homepage parent){
		this.parent = parent;
		createAndShowRegisterPanel();
	}
	
	private void createAndShowRegisterPanel() {
		setSize(600, 600);
		setLayout(null);
		nameLabel = new JLabel("Imię");
		surnameLabel = new JLabel("Nazwisko");
		loginLabel = new JLabel("Login");
		passLabel = new JLabel("Hasło");
		emailLabel = new JLabel("E-mail");
		streetLabel = new JLabel("Ulica");
		houseLabel = new JLabel("Nr domu");
		flatLabel = new JLabel("Nr mieszkania");
		phoneLabel = new JLabel("Telefon");
		cityLabel = new JLabel("Miasto");
		postCodeLabel = new JLabel("Kod pocztowy");
		name = new JTextField();
		surname = new JTextField();
		login= new JTextField();
		password= new JPasswordField();
		email= new JTextField();
		phoneNumber= new JTextField();
		street= new JTextField();
		houseNumber= new JTextField();
		flatNumber= new JTextField();
		city= new JTextField();
		postCode= new JTextField();
		confirmRegister = new JButton("Rejestruj");
		errorLabel = new JLabel();
		infoLabel = new JLabel("Proszę wypełnić dane do rejestracji. Wszystkie pola są obowiązkowe.");
		addComponent(infoLabel, 20, 10, 500, 20);
		addComponent(nameLabel, 20, 40, 120, 30);
		addComponent(name, 160, 40, 100, 30);
		addComponent(surnameLabel, 20, 80, 120, 30);
		addComponent(surname, 160, 80, 100, 30);
		addComponent(loginLabel, 20, 120, 120, 30);
		addComponent(login, 160, 120, 100, 30);
		addComponent(passLabel, 20, 160, 120, 30);
		addComponent(password, 160, 160, 100, 30);
		addComponent(emailLabel, 20, 200, 120, 30);
		addComponent(email, 160, 200, 100, 30);
		addComponent(phoneLabel, 20, 240, 120, 30);
		addComponent(phoneNumber, 160, 240, 100, 30);
		addComponent(streetLabel, 320, 40, 120, 30);
		addComponent(street, 460, 40, 100, 30);
		addComponent(houseLabel, 320, 80, 120, 30);
		addComponent(houseNumber, 460, 80, 100, 30);
		addComponent(flatLabel, 320, 120, 120, 30);
		addComponent(flatNumber, 460, 120, 100, 30);
		addComponent(cityLabel, 320, 160, 120, 30);
		addComponent(city, 460, 160, 100, 30);
		addComponent(postCodeLabel, 320, 200, 120, 30);
		addComponent(postCode, 460, 200, 100, 30);
		addComponent(confirmRegister, 250, 300, 100, 30);
		addComponent(errorLabel, 20, 350, 500, 30);
		errorLabel.setVisible(false);
		
		registration = new Registration();
		setActionListener();
	}
	
	private void setActionListener(){
		confirmRegister.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				try { 
					if (registration.register(name.getText(), surname.getText(), login.getText(), String.valueOf(password.getPassword()), new Byte("1"), email.getText(), phoneNumber.getText(), street.getText(), Integer.parseInt(houseNumber.getText()), Integer.parseInt(flatNumber.getText()), city.getText(), postCode.getText())){
						parent.registerMsg();
					}
					else{
						errorLabel.setText("Rejestracja się nie udała, pewne pola są błędnie wypełnione");
						errorLabel.setForeground(Color.RED);
						errorLabel.setFont(new Font("Arial", Font.BOLD, 15));
						if (!errorLabel.isVisible())
							errorLabel.setVisible(true);
					}
				} catch (NumberFormatException e1) {
					// TODO Auto-generated catch block
					errorLabel.setText("Pewne pola są puste, bądź nieprawidłowo wypełnione");
					errorLabel.setForeground(Color.RED);
					errorLabel.setFont(new Font("Arial", Font.BOLD, 15));
					if (!errorLabel.isVisible())
						errorLabel.setVisible(true);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
	}
	
	private void addComponent(JComponent comp, int x, int y, int width, int height){
		add(comp);
		comp.setBounds(x, y, width, height);
	}
	
	public void clearFields(){
		name.setText("");
		surname.setText("");
		login.setText("");
		password.setText("");
		street.setText("");
		phoneNumber.setText("");
		flatNumber.setText("");
		houseNumber.setText("");
		postCode.setText("");
		city.setText("");
		email.setText("");
		errorLabel.setText("");
	}
	
	//Panel rejestracyjny
		
		private JLabel nameLabel;
		private JLabel surnameLabel;
		private JLabel loginLabel;
		private JLabel passLabel;
		private JLabel emailLabel;
		private JLabel streetLabel;
		private JLabel houseLabel;
		private JLabel flatLabel;
		private JLabel phoneLabel;
		private JLabel cityLabel;
		private JLabel postCodeLabel;
		private JTextField name;
		private JTextField surname;
		private JTextField login;
		private JPasswordField password;
		private JTextField email;
		private JTextField phoneNumber;
		private JTextField street;
		private JTextField houseNumber;
		private JTextField flatNumber;
		private JTextField city;
		private JTextField postCode;
		private JButton confirmRegister;
		private JLabel infoLabel;
		private JLabel errorLabel;
		
		//Metody do rejestracji
		Registration registration;
		Homepage parent;
}
